package huixue.cn.com.library.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/8/1.
 */

public class BaseRecycleView extends RecyclerView {

    private View mHeaderView;
    private View mFooterView;
    private View mEmptyView;
    private BaseRecycleAdapter mGloriousAdapter;

    public BaseRecycleView(Context context) {
        super(context);
    }

    public BaseRecycleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void addHeaderView(View view){
        mHeaderView = view;
        mGloriousAdapter.notifyItemInserted(0);
    }

    public void addFooterView(View view) {
        mFooterView = view;
        mGloriousAdapter.notifyItemInserted(mGloriousAdapter.getItemCount() - 1);
    }

    public void setEmptyView(View view) {
        mEmptyView = view;
        mGloriousAdapter.notifyDataSetChanged();
    }

    @Override
    public void setAdapter(Adapter adapter) {
        if (adapter != null) {
            mGloriousAdapter = new BaseRecycleAdapter(adapter);
        }
        super.setAdapter(mGloriousAdapter);
    }

    private class BaseRecycleAdapter extends RecyclerView.Adapter<ViewHolder>{

        private Adapter mOriginalAdapter;
        private int ITEM_TYPE_NORMAL = 0;
        private int ITEM_TYPE_HEADER = 1;
        private int ITEM_TYPE_FOOTER = 2;
        private int ITEM_TYPE_EMPTY = 3;

        public BaseRecycleAdapter(Adapter mOriginalAdapter) {
            this.mOriginalAdapter = mOriginalAdapter;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if(viewType == ITEM_TYPE_HEADER){
                return new BaseViewHolder(mHeaderView);
            }else if(viewType == ITEM_TYPE_EMPTY){
                return new BaseViewHolder(mEmptyView);
            }else if(viewType == ITEM_TYPE_FOOTER){
                return new BaseViewHolder(mFooterView);
            }else{
                return mOriginalAdapter.onCreateViewHolder(parent, viewType);
            }
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            int type = getItemViewType(position);
            if (type == ITEM_TYPE_HEADER || type == ITEM_TYPE_FOOTER || type == ITEM_TYPE_EMPTY) {
                return;
            }
            int realPosition = getRealItemPosition(position);
            mOriginalAdapter.onBindViewHolder(holder, realPosition);
        }

        public int getRealItemPosition(int position){
            if(mHeaderView!=null){
                position = position -1;
            }
            return position;
        }

        @Override
        public int getItemCount() {
            int itemCount = mOriginalAdapter.getItemCount();
            //加上其他各种View
            if (null != mEmptyView && itemCount == 0) itemCount++;
            if (null != mHeaderView) itemCount++;
            if (null != mFooterView) itemCount++;
            return itemCount;
        }

        @Override
        public int getItemViewType(int position) {
            //return super.getItemViewType(position);
            if(mHeaderView!=null && position==0){
                return ITEM_TYPE_HEADER;
            }else if(mFooterView!=null && position==(getItemCount()-1)){
                return ITEM_TYPE_FOOTER;
            }else if(mEmptyView!=null && mOriginalAdapter.getItemCount() == 0){
                return ITEM_TYPE_EMPTY;
            }else {
                return ITEM_TYPE_NORMAL;
            }
        }

        class BaseViewHolder extends ViewHolder {
            BaseViewHolder(View itemView) {
                super(itemView);
            }
        }

    }


}
